<template>
  <div class="app-container">
    <!-- <div id="map"></div> -->
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="适用区域" prop="regionName">
        <el-input
          v-model="queryParams.regionName"
          placeholder="适用区域"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="归属门店" prop="storeName">
        <el-input
          v-model="queryParams.storeName"
          placeholder="请输入归属门店"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户姓名" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入客户姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系电话" prop="phoneNumber">
        <el-input
          v-model="queryParams.phoneNumber"
          placeholder="请输入联系电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="消费时间" prop="buyTime">
        <el-date-picker
          v-model="queryParams.buyTime"
          type="date"
          placeholder="选择日期"
          value-format="yyyy-MM-dd"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          v-hasPermi="['business:storeCardDetail:query']"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
    <right-toolbar
      :showSearch.sync="showSearch"
      @queryTable="getList"
    ></right-toolbar>

    <el-table
      v-loading="loading"
      :data="storeList"
      max-height="650px"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        label="订单号"
        align="center"
        prop="orderId"
        width="160"
      />
      <el-table-column
        label="适用区域"
        align="center"
        prop="ableUseRegionName"
        width="160"
      />
      <el-table-column
        label="适用门店"
        align="center"
        prop="ableUseStoreName"
        width="160"
      />
      <el-table-column
        label="客户姓名"
        align="center"
        prop="userName"
        width="160"
      />
      <el-table-column
        label="电话"
        align="center"
        prop="phoneNumber"
        width="160"
      />
      <el-table-column
        label="储值卡名称"
        align="center"
        prop="ruleName"
        width="160"
      />

      <el-table-column
        label="消费区域"
        align="center"
        prop="regionName"
        width="160"
      />
      <el-table-column
        label="消费门店"
        align="center"
        prop="storeName"
        width="160"
      />

      <el-table-column label="项目名称" align="center" prop="itemNames" />
      <el-table-column label="附加商品名称" align="center" prop="productName" />
      <el-table-column label="包间名称" align="center" prop="compartmentName" />

      <el-table-column label="原本金" align="center" prop="prevPrincipal" />
      <el-table-column label="原赠金" align="center" prop="prevGift" />

      <el-table-column label="变动本金" align="center" prop="principal" />
      <el-table-column label="变动赠金" align="center" prop="gift" />

      <el-table-column label="剩余本金" align="center" prop="prevPrincipal" >
        <template slot-scope="scope">
              {{ (Number(scope.row.prevPrincipal==undefined?0:scope.row.prevPrincipal)+ 
           Number(scope.row.principal==undefined?0:scope.row.principal)).toFixed(2)}}
           </template>
      </el-table-column>
      <el-table-column label="剩余赠金" align="center" prop="prevGift" >
        <template slot-scope="scope">
              {{ (Number(scope.row.prevGift==undefined?0:scope.row.prevGift)+ 
           Number(scope.row.gift==undefined?0:scope.row.gift)).toFixed(2)}}
           </template>
      </el-table-column>

      <!-- <el-table-column label="变动后本金" align="center" prop="remainPrincipal" />
      <el-table-column label="变动后赠金" align="center" prop="remainGift" /> -->

      <!-- <el-table-column label="剩余总额" align="center" prop="remainPrincipal" >
        <template slot-scope="scope">
              {{ (Number(scope.row.remainPrincipal==undefined?0:scope.row.remainPrincipal)+ 
           Number(scope.row.remainGift==undefined?0:scope.row.remainGift))}}
           </template>
      </el-table-column> -->

      <!-- <el-table-column
        label="推荐人"
        align="center"
        prop="referrerName"
        width="160"
      />
      <el-table-column
        label="推荐人电话 "
        align="center"
        prop="referrerNum"
        width="160"
      /> -->
      <!-- <el-table-column label="购买时间 " align="center" prop="detailType" width="150"/> -->
      <el-table-column label="详情类型" align="center" prop="detailType">
        <template slot-scope="scope">
          <span>{{ detailType[scope.row.detailType] }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="支付方式 " align="center" prop="paymentType">
        <template slot-scope="scope">
          {{paymentType[scope.row.paymentType]  }}
        </template>
      </el-table-column> -->
      <el-table-column label="消费时间" align="center" prop="createTime" />

      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        fixed="right"
        width="150"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['business:storeCardDetail:edit']"
            >查看</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['business:storeCardDetail:del']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 添加或修改门店对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="600px"
      append-to-body
      :close-on-click-modal="false"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="储蓄卡编号" prop="carId">
          <el-input
            v-model="form.carId"
            :disabled="true"
            placeholder="请输入储蓄卡编号"
            style="width: 222px"
          />
        </el-form-item>
        <el-form-item label="创建时间" prop="createTime">
          <!-- <el-input v-model="form.buyTime" placeholder="请输入创建时间" /> -->
          <el-date-picker
            :disabled="true"
            v-model="form.createTime"
            type="date"
            placeholder="请输入创建时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="适用区域" prop="regionId">
          <el-select clearable v-model="form.regionId" :disabled="true">
            <el-option
              v-for="(item, index) of regionOption"
              :key="index"
              :label="item.value"
              :value="item.key"
              >{{ item.value }}</el-option
            >
          </el-select>
        </el-form-item>
        <el-form-item label="适用门店" prop="storeId">
          <el-select clearable v-model="form.storeId" :disabled="true">
            <el-option
              v-for="(item, index) of storeOption"
              :key="index"
              :label="item.value"
              :value="item.key"
              >{{ item.value }}</el-option
            >
          </el-select>
        </el-form-item>
        <el-form-item label="客户姓名" prop="userName">
          <el-input
            v-model="form.userName"
            :disabled="true"
            placeholder="请输入客户姓名"
            style="width: 222px"
          />
        </el-form-item>
        <el-form-item label="电话" prop="phoneNumber">
          <el-input
            v-model="form.phoneNumber"
            :disabled="true"
            placeholder="请输入电话"
            style="width: 222px"
          />
        </el-form-item>
        <!-- <el-form-item label="会员卡规则名称" prop="ruleId">
          <el-select clearable v-model="form.ruleId" >
					<el-option v-for="(item,index) of ruleOption" :key="index" :label="item.value" :value="item.key">{{item.value}}</el-option>
				</el-select>
      </el-form-item> -->
        <el-form-item label="使用金额" prop="principal">
          <el-input
            v-model="form.principal"
            :disabled="true"
            placeholder="请输入充值金额"
            style="width: 222px"
          />
        </el-form-item>
        <!-- <el-form-item label="赠送金额" prop="gift">
          <el-input v-model="form.gift" placeholder="请输入赠送金额" style="width: 222px;"/>
        </el-form-item> -->
        <el-form-item label="总金额" prop="sumMoney">
          <el-input
            v-model="form.sumMoney"
            :disabled="true"
            placeholder="请输入总金额"
            style="width: 222px"
          />
        </el-form-item>
        <el-form-item label="推荐人" prop="referrerName">
          <el-input
            v-model="form.referrerName"
            :disabled="true"
            placeholder="请输入推荐人"
            style="width: 222px"
          />
        </el-form-item>
        <el-form-item label="推荐人电话" prop="referrerNum">
          <el-input
            v-model="form.referrerNum"
            :disabled="true"
            placeholder="请输入推荐人电话"
            style="width: 222px"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listCardDetail,
  getCardDetail,
  delCardDetail,
  addStore,
  updateStore,
  listMemberRegion,
} from "@/api/system/card/storeCardDetail";
import { liststoreList } from "@/api/system/personnelMatters/arrange";

export default {
  name: "Store",
  data() {
    return {
      // map: null,
      local: null,
      mk: null,
      latitude: "",
      longitude: "",
      keyWords: "",
      result: "",
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 门店表格数据
      storeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        regionName: null,
        storeName: null,
        userName: null,
        phoneNumber: null,
        buyTime: null,
        // province: null,
        // provinceString: null,
        // city: null,
        // cityString: null,
        // county: null,
        // countyString: null,
        // detail: null,
        // longitude: null,
        // latitude: null,
        // intro: null,
        // images: null,
        // status: null,
        // isInitiate: null,
        // openingTime: null,
        // closingTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      options: [],
      List: [
        {
          key: 1,
          value: "营业中",
        },
        {
          key: 2,
          value: "未营业",
        },
      ],
      regionOption: [],
      storeOption: [],
      ruleOption: [],
      detailType: ["", "充值", "消费", "退卡", "转赠", "退单"],
      paymentType: ["", "微信", "支付宝", "现金", "银行卡", "其他"],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    getRegionOption() {
      listMemberRegion().then((res) => {
        this.regionOption = res.data.map((rItem) => {
          return {
            key: rItem.id,
            value: rItem.regionName,
          };
        });
      });
    },
    getStoreOption() {
      liststoreList().then((res) => {
        this.storeOption = res.rows.map((rItem) => {
          return {
            key: rItem.id,
            value: rItem.storeName,
          };
        });
      });
    },
    /** 查询门店列表 */
    getList() {
      this.loading = true;
      listCardDetail(this.queryParams).then((response) => {
        const table = response.rows;
        table.forEach((item) => {
          switch (item.referrerName) {
            case null:
              item.referrerName = "暂无";
              break;
            default:
              break;
          }
          switch (item.referrerNum) {
            case null:
              item.referrerNum = "暂无";
              break;
            default:
              break;
          }
        });
        this.storeList = table;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        storeName: null,
        phoneNumber: null,
        province: null,
        provinceString: null,
        city: null,
        cityString: null,
        county: null,
        countyString: null,
        detail: null,
        longitude: null,
        latitude: null,
        intro: null,
        images: null,
        status: null,
        isInitiate: null,
        openingTime: null,
        closingTime: null,
        createTime: null,
        updateTime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      console.log("queryParams.buyTime-----", this.queryParams.buyTime);
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        regionName: null,
        storeName: null,
        userName: null,
        phoneNumber: null,
        buyTime: null,
      };
      // this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加储蓄卡明细";
    },
    // 上下架
    handleGround(row, statue) {
      const params = {
        id: row.id,
        statue: statue,
      };
      this.$confirm(
        `此操作将${statue === 1 ? "上架" : "下架"}该数据, 是否继续?`,
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(() => {
          updateCase(params).then((res) => {
            this.$message({
              type: "success",
              message: `${statue === 1 ? "上架" : "下架"}成功!`,
            });
            this.getList();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消上下架",
          });
        });
    },
    /** 修改按钮操作 */
    async handleUpdate(row) {
      this.reset();
      await this.getRegionOption();
      await this.getStoreOption();
      const id = row.id || this.ids;
      getCardDetail(id).then((response) => {
        this.form = response.data;
        this.form.regionId = response.data.regionId * 1;
        if (response.data.referrerName == null) {
          response.data.referrerName = "暂无";
        }
        if (response.data.referrerNum == null) {
          response.data.referrerNum = "暂无";
        }
        this.open = true;
        this.title = "修改门店";
      });
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateStore(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addStore(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    handReset(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认重置编号为"' + ids + '"的数据项？')
        .then(function () {
          return listResetPwd({ userId: ids });
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("重置成功");
        })
        .catch(() => {});
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除编号为"' + ids + '"的数据项？')
        .then(function () {
          return delCardDetail(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/store/export",
        {
          ...this.queryParams,
        },
        `store_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
